home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / timerinit.c < prev    next >
C/C++ Source or Header  |  1994-03-29  |  7KB  |  222 lines

  1. /* $Id: timerinit.c,v 3.1 1994/03/29 12:56:35 ppessi Exp $
  2.  *
  3.  * timerinit.c --- SAS C auto initialization functions for timer device
  4.  *
  5.  * Authors: ppessi <Pekka.Pessi@hut.fi>, jraja <Jarno.Rajahalme@hut.fi>
  6.  *
  7.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *                  All rights reserved.
  10.  *
  11.  * Created      : Sat Mar 20 03:31:29 1993 ppessi
  12.  * Last modified: Mon Mar 28 02:17:16 1994 ppessi
  13.  *
  14.  * $Log: timerinit.c,v $
  15.  * Revision 3.1  1994/03/29  12:56:35  ppessi
  16.  * Fixed some SAS C 6.50 incompatibilities
  17.  *
  18.  * Revision 1.2  1994/02/17  09:42:00  ppessi
  19.  * Fixed return type of autoinitialization function (return 0 on success)
  20.  *
  21.  * Revision 1.1  1993/10/18  06:22:50  jraja
  22.  * Initial revision
  23.  */
  24.  
  25. /****** net.lib/autoinit_timer.device *****************************************
  26.  
  27.     NAME
  28.         timerinit - SAS C Autoinitialization Functions for timer.device
  29.  
  30.     SYNOPSIS
  31.         _STIopenTimer()
  32.  
  33.         LONG _STI_200_openTimer(void)
  34.  
  35.         _STDcloseTimer()
  36.  
  37.         void _STD_200_closeTimer(void)
  38.  
  39.     FUNCTION
  40.  
  41.         These functions open and close the timer.device at the startup and
  42.         exit of the program, respectively. For a program to use these
  43.         functions, it must be linked with netlib:net.lib.
  44.  
  45.         The opened device base is stored in the TimerBase global variable.
  46.  
  47.         If the device can be opened, the _STIopenTimer() sets up the time zone
  48.         information, which is used by the gettimeofday() function.
  49.  
  50.     NOTES
  51.         The time zone information is got from the environment variable named
  52.         TZ. The format for this variable is:
  53.  
  54.             zzznnnddd
  55.  
  56.         where zzz is three letter identifier for the time zone (for example
  57.         GMT), and the nnn is hours west from Greenwich on range [-23,24]
  58.         (negative values are to east).  The last field is the abbreviation for
  59.         the local daylight saving time zone (which is not interpreted by this
  60.         version).
  61.  
  62.         If the TZ environment variable cannot be found, Greenwich Mean Time
  63.         (GMT) is used instead.
  64.  
  65.         The autoinitialization and autotermination functions are features
  66.         specific to the SAS C6.  However, these functions can be used with
  67.         other (ANSI) C compilers, too.  Example follows:
  68.  
  69.         \* at start of main() *\
  70.  
  71.         atexit(_STDcloseTimer);
  72.         _STDopenTimer();
  73.  
  74.     BUGS
  75.         TZ "hours west from GMT" should be interpreted as float.
  76.  
  77.         The same autoinitialization won't work for both SAS C 6.3 and SAS C
  78.         6.50 or latter.  Only way to terminate an initialization function is
  79.         by exit() call with SAS C 6.3 binary.  If an autoinitialization
  80.         function is terminated by exit() call with SAS C 6.50 binary, the
  81.         autotermination functions won't be called.  Due this braindamage
  82.         these compilers require separate net.lib libraries.
  83.  
  84.     SEE ALSO
  85.         net.lib/gettimeofday(),
  86.         SAS/C 6 User's Guide p. 145 for details of autoinitialization and
  87.         autotermination functions.
  88. *****************************************************************************
  89. *
  90. */
  91.  
  92. #include <exec/types.h>
  93. #include <exec/devices.h>
  94. #include <dos/dos.h>
  95. #include <devices/timer.h>
  96. #include <proto/exec.h>
  97. #include <proto/dos.h>
  98.  
  99. #include <stdlib.h>
  100. #include <sys/time.h>
  101.  
  102. /* SAS C 6.50 kludge */
  103. #if __VERSION__ > 6 || __REVISION__ >= 50
  104. #define exit(x) return(x)
  105. #endif
  106.  
  107. struct Device *TimerBase = 0L;
  108. static void *unit;
  109.  
  110. /*
  111.  * Time zone support for the gettimeofday. Zeroes default to the GMT
  112.  * without daylight saving.
  113.  */
  114. struct timezone __time_zone = {0,0};
  115.  
  116. /*
  117.  * Seconds to to the system time (seconds from 00:00 1.1.1978) 
  118.  * to the GMT (seconds from 00:00 1.1.1970).
  119.  * _STIopenTimer() adds the local time seconds west from GMT to this
  120.  * value, so the local time gets converted to the GMT.
  121.  */
  122. long __local_to_GMT = ((8L*365 + 8/4)*24*60*60);
  123.  
  124. /* 
  125.  * Locale information is included here, since the 2.1 includes may be hard 
  126.  * to find.
  127.  */
  128.  
  129. /* This structure must only be allocated by locale.library and is READ-ONLY! */
  130. struct Locale
  131. {
  132.     STRPTR    loc_LocaleName;      /* locale's name         */
  133.     STRPTR    loc_LanguageName;      /* language of this locale     */
  134.     STRPTR    loc_PrefLanguages[10];      /* preferred languages     */
  135.     ULONG    loc_Flags;          /* always 0 for now         */
  136.  
  137.     ULONG    loc_CodeSet;          /* always 0 for now         */
  138.     ULONG    loc_CountryCode;      /* user's country code     */
  139.     ULONG    loc_TelephoneCode;      /* country's telephone code     */
  140.     LONG    loc_GMTOffset;          /* minutes from GMT         */
  141.  
  142. /* deleted the rest to save space */
  143. };
  144.  
  145. void CloseLocale( struct Locale *locale );
  146. struct Locale *OpenLocale( STRPTR name );
  147.  
  148. #pragma libcall LocaleBase CloseLocale 2A 801
  149. #pragma libcall LocaleBase OpenLocale 9C 801
  150.  
  151. LONG __stdargs
  152. _STI_200_openTimer(void)
  153. {
  154.   struct timerequest dummyTimer = { 0 };
  155.  
  156.   if (!TimerBase && !OpenDevice("timer.device", UNIT_VBLANK, 
  157.                 (struct IORequest *)&dummyTimer, 0L)) {
  158.     TimerBase = dummyTimer.tr_node.io_Device;
  159.     unit = dummyTimer.tr_node.io_Unit;
  160.     if (TimerBase->dd_Library.lib_Version >= 36) {
  161.       /*
  162.        * Initialize time zone information for the gettimeofday()
  163.        * First try to open locale (2.1 and up), and if that fails,
  164.        * try to read environment variable TZ.
  165.        */
  166.       void *LocaleBase;
  167.       struct Locale *thisLocale = NULL;
  168.  
  169.       if ((LocaleBase = OpenLibrary("locale.library", 38)) != NULL) {
  170.     if ((thisLocale = OpenLocale(NULL)) != NULL) {
  171.       /*
  172.        * Update time zone minutes west from GMT.
  173.        */
  174.       __time_zone.tz_minuteswest = thisLocale->loc_GMTOffset;
  175.       CloseLocale(thisLocale);
  176.     }
  177.     CloseLibrary(LocaleBase);
  178.       }
  179.       if (!thisLocale) { /* if locale information was not available */
  180.     short len;
  181.     long value;
  182.     char zone[10];
  183.     BPTR file = Open("ENV:TZ", MODE_OLDFILE);
  184.     if (file) {
  185.       len = Read(file, zone, sizeof(zone));
  186.       if (len > 3) {
  187.         zone[len] = '\000';
  188.         /* should interpret floats as well! */
  189.         if (StrToLong(zone+3, &value) > 0) {
  190.           /*
  191.            * Update time zone minutes west from GMT.
  192.            */
  193.           __time_zone.tz_minuteswest = (short)value * (short)60;
  194.         }
  195.       }
  196.       Close(file);
  197.     }
  198.       }
  199.  
  200.       /*
  201.        * Update local time seconds to GMT translation
  202.        */
  203.       __local_to_GMT += (short)__time_zone.tz_minuteswest * (short)60;
  204.  
  205.       return 0;
  206.     }
  207.   }
  208.   exit(20);
  209. }
  210.  
  211. void __stdargs
  212. _STD_200_closeTimer(void)
  213. {
  214.   struct timerequest dummyTimer = { 0 };
  215.   if (!TimerBase)
  216.     return;
  217.  
  218.   dummyTimer.tr_node.io_Device = TimerBase;
  219.   dummyTimer.tr_node.io_Unit = unit;
  220.   CloseDevice((struct IORequest*)&dummyTimer);
  221. }
  222.